<template>
  <view class="router-view bcl-active">
    <div class="mb-10" v-for="(item, index) in list" :key="index">
      <div
        class="sticky container bcl-white"
        @click="item.showList = !item.showList"
        v-if="item.date"
      >
        <div class="box box-column-center">
          <span class="fz-15 bold mr-5">{{ item.date }}</span>
          <span
            class="icon icon-arrow-right"
            :class="{ 'show-list': item.showList }"
          ></span>
        </div>
        <div class="box">
          <div class="mr-10">
            <span class="cl-second">支出：</span>
            <span>￥{{ item.out_total }}</span>
          </div>
          <div>
            <span class="cl-second">收入：</span>
            <span>￥{{ item.in_total }}</span>
          </div>
        </div>
      </div>

      <div
        class="ul container bcl-active"
        style="padding-bottom: 0;"
        v-if="item.showList"
      >
        <div class="item" v-for="(citem, cindex) in item.list" :key="cindex">
          <trade-item :item="citem"></trade-item>
        </div>
      </div>
    </div>

    <!-- 数据状态 -->
    <uni-load-more
      :status="loadMoreState"
      @retry="getTradeRecords(true)"
    ></uni-load-more>
  </view>
</template>

<script>
import { walletTradeLog, bondLog } from "@/apis/user";
import tab from "@/components/tab";
import tradeItem from "./components/tradeItem";
import uniLoadMore from "@/components/loadMore";

export default {
  components: {
    tab,
    tradeItem,
    uniLoadMore,
  },

  data() {
    return {
      loadMoreState: 0,
      list: [],
      total: {}, // 收入和支出
      pageType: "balance",
    };
  },

  onLoad(options) {
    // 页面类型 options.type = balance 为钱包明细， bond 为保证金明细
    this.pageType = options.type;
    this.getTradeRecords();
  },

  methods: {
    /**
     * 获取列表
     */
    async getTradeRecords(refresh = false) {
      this.loadMoreState = 0;

      // 列表请求的方法，对应 pageType; balance 为 钱包明细， bond为保证金明细
      let listFn = {
        balance: walletTradeLog,
        bond: bondLog,
      };

      let params = {};

      try {
        let { code, data, message } = await listFn[this.pageType](params);

        if (code === 200) {
          data.forEach((item) => {
            item.showList = true;
          });

          this.list = data;

          if (this.list.length) this.loadMoreState = 3;
          else this.loadMoreState = 4;
        } else {
          this.loadMoreState = 2;
        }
      } catch (e) {
        this.loadMoreState = 2;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.ul {
  .item {
    &:not(:last-child) {
      margin-bottom: 20rpx;
    }
  }
}

.icon-arrow-right {
  transform: rotateZ(90deg);

  &.show-list {
    transform: rotateZ(270deg);
  }
}

.sticky {
  position: sticky;
  top: var(--window-top);
  left: 0;
  width: 100%;
}
</style>
